package com.easy.web.service.impl;

import com.easy.web.mapper.SlideMapper;
import com.easy.web.model.Slide;
import com.easy.web.model.SlideExample;
import com.easy.web.service.SlideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/12/29.
 */
@Service
@Transactional
public class SlideServiceImpl implements SlideService{

    @Autowired
    private SlideMapper slideMapper;

    /**
     * 查询幻灯片列表
     */
    @Override
//    @Cacheable(value = "Slide",key = "0")
    public List<Slide> getSlideList() {
        return getSlideListDB();
    }

    /**
     * 新增幻灯片
     *
     * @param slide
     */
    @Override
//    @CachePut(value = "Slide",key = "0")
    public List<Slide> addSlide(Slide slide) {
        slideMapper.insert(slide);
        return getSlideListDB();
    }

    /**
     * 删除一个幻灯片
     *
     * @param id
     */
    @Override
//    @CachePut(value = "Slide",key = "0")
    public List<Slide> delSlide(String id) {
        slideMapper.deleteByPrimaryKey(id);
        return getSlideListDB();
    }

    /**
     * 根据顺序查询
     *
     * @param seq
     */
    @Override
    public Slide querySlideBySeq(String seq) {
        SlideExample slideExample=new SlideExample();
        slideExample.createCriteria().andSeqEqualTo(seq);
        List<Slide>  slideList=slideMapper.selectByExample(slideExample);
        if(slideList.size()>0){
            return slideList.get(0);
        }
        return null;
    }


    /**
     * 更新幻灯片信息
     *
     * @param slide
     */
    @Override
//    @CachePut(value = "Slide",key = "0")
    public List<Slide> updateSlide(Slide slide) {
        slideMapper.updateByPrimaryKeySelective(slide);
        return getSlideListDB();
    }


    /**
     * 根据id查询幻灯片
     *
     * @param id
     */
    @Override
    public Slide querySlideById(String id) {
        return slideMapper.selectByPrimaryKey(id);
    }

    public List<Slide> getSlideListDB() {
        SlideExample slideExample=new SlideExample();
        slideExample.setOrderByClause("seq");
        return slideMapper.selectByExample(slideExample);
    }

}
